package yf.mall.service.impl;

import yf.mall.common.Const;
import yf.mall.dao.MallGoodsDao;
import yf.mall.entity.MallGoodsEntity;
import com.yf.response.RRException;
import com.yf.service.impl.BaseServiceImpl;
import yf.mall.entity.MallOrderEntity;
import yf.mall.entity.OrderItemEntity;
import yf.mall.enums.MallOrderOrderFromEnum;
import yf.mall.enums.MallOrderOrderStatusEnum;
import yf.mall.enums.MallOrderOrderTypeEnum;
import yf.mall.service.MallOrderService;
import yf.mall.service.OrderItemService;
import yf.mall.vo.ItemVo;
import yf.mall.vo.MallStoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import yf.mall.dao.MallStoreDao;
import yf.mall.entity.MallStoreEntity;
import yf.mall.service.MallStoreService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品库存Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Sun Sep 29 09:50:50 CST 2019
 */
@Service
public class MallStoreServiceImpl extends BaseServiceImpl<MallStoreEntity> implements MallStoreService {

    private MallStoreDao mallStoreDao;

    @Autowired
    private MallGoodsDao mallGoodsDao;  

    @Autowired
    private MallOrderService orderService;

    @Autowired
    private OrderItemService itemService;


    @Autowired
    public void setDao(MallStoreDao mallStoreDao) {
        this.mallStoreDao = mallStoreDao;
        super.setBaseDao(mallStoreDao);
    }


//    @Override
//    public List<MallStoreVo> queryListVo(MallStoreVo mallStoreVo) {
//        return mallStoreDao.queryListVo(mallStoreVo);
//    }
//
//    @Override
//    public MallStoreVo getVo(Long id) {
//        return mallStoreDao.getVo(id);
//    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/


    @Override
    @Transactional(rollbackFor = Exception.class)
    public MallStoreEntity increase(Long userId, Long goodsId, Integer quantity) {
        MallStoreEntity mallStoreEntity = mallStoreDao.findByUserIdAndGoodsId(userId, goodsId);
        if (mallStoreEntity == null) {
            mallStoreEntity = new MallStoreEntity(userId, goodsId, null, 0, 0);
            mallStoreDao.save(mallStoreEntity);
        }
        mallStoreEntity.setQuantity(mallStoreEntity.getQuantity() + quantity);
        //增加
        mallStoreDao.increase(userId, goodsId, quantity);
        return mallStoreEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MallStoreEntity decrease(Long userId, Long goodsId, Integer quantity) {
        MallStoreEntity mallStoreEntity = mallStoreDao.findByUserIdAndGoodsId(userId, goodsId);
        MallGoodsEntity mallGoodsEntity = mallGoodsDao.get(goodsId);
        if (mallStoreEntity == null) {
            mallStoreEntity = new MallStoreEntity(userId, goodsId, null, 0, 0);
            mallStoreDao.save(mallStoreEntity);
        }
        if (mallStoreEntity.getQuantity() - quantity < 0) {
            throw new RRException(mallGoodsEntity.getName() + "账户余额不足");
        }
        //减少 TODO 事务问题  会造成负数
        mallStoreDao.decrease(userId, goodsId, quantity);
        mallStoreEntity.setQuantity(mallStoreEntity.getQuantity() - quantity);
        return mallStoreEntity;
    }



    //后台发货的退回库存 相关的还货退回
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void backDelivery(String orderNo) {
        MallOrderEntity orderEntity = orderService.findByOrderNo(orderNo);
        //后台发货 商城发货的直接退钱了不用退货
            List<OrderItemEntity> items = itemService.findByOrderId(orderEntity.getId());
            items.forEach(item -> {
                mallStoreDao.increase(orderEntity.getUserId(), item.getGoodsId(), item.getQuantity());
            });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void backCod(String orderNo) {
        //还货的
        List<MallOrderEntity> orderEntities = orderService.findReturn(orderNo);
        orderEntities.forEach(orderEntity1 -> {
            back(orderEntity1);
        });
    }

    /**
     * TODO 追回进货券
     * @param orderNo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void backAutoBuy(String orderNo) {
        //自动购买的
        List<MallOrderEntity> orderEntities = orderService.findAutoBuy(orderNo);
        orderEntities.forEach(orderEntity1 -> {

        });
    }

    //生成逆向退回订单
    private void back(MallOrderEntity orderEntity){
        List<OrderItemEntity> items = itemService.findByOrderId(orderEntity.getId());
        orderEntity = orderService.buildBackOrder(orderEntity);
        orderService.save(orderEntity);
        for (OrderItemEntity item : items){
            item.setId(null);
            item.setOrderId(orderEntity.getId());
            itemService.save(item);
            //库存退回
            if(!orderEntity.getUserId().equals(Const.ADMIN_ID)){
                increase(orderEntity.getUserId(),item.getGoodsId(),item.getQuantity());
            }
            //库存减少
            if(!orderEntity.getFromUserId().equals(Const.ADMIN_ID)){
                decrease(orderEntity.getUserId(),item.getGoodsId(),item.getQuantity());
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transfer(Long orderId) {
        MallOrderEntity orderEntity = orderService.get(orderId);
        List<OrderItemEntity> items = itemService.findByOrderId(orderId);
        items.forEach(item->{
            decrease(orderEntity.getFromUserId(),item.getGoodsId(),item.getQuantity());
            //转货订单 到账人要增加
            if(MallOrderOrderTypeEnum.TRANSFER.getIndex()==orderEntity.getOrderType()){
                increase(orderEntity.getUserId(),item.getGoodsId(),item.getQuantity());
            }
        });
    }

    @Override
    public List<MallStoreVo> findByUserId(Long userId) {
        return mallStoreDao.findByUserId(userId);
    }
}
